package com.jswlw.service.biz.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jswlw.common.utils.StringUtils;
import com.jswlw.dao.biz.DevTypeMapper;
import com.jswlw.entity.biz.BillSet;
import com.jswlw.entity.biz.DevType;
import com.jswlw.entity.biz.FilterInfo;
import com.jswlw.service.biz.BillSetService;
import com.jswlw.service.biz.DevTypeService;
import com.jswlw.service.biz.FilterInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Service
public class DevTypeServiceImpl implements DevTypeService {

    @Autowired
    DevTypeMapper mapper;

    @Autowired
    FilterInfoService filterInfoService;

    @Autowired
    BillSetService billSetService;

    @Override
    public void add(DevType devType) {
        mapper.insert(devType);
    }

    @Override
    public void update(DevType devType) {
        mapper.updateByPrimaryKeySelective(devType);
    }

    @Override
    public void delete(int id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public DevType find4Id(int id) {
        DevType devType = mapper.selectByPrimaryKey(id);
        if (Objects.nonNull(devType)) {
            List<FilterInfo> filterInfoList = new ArrayList<>();
            List<Integer> filterIdList = new ArrayList<>();
            if (StringUtils.isNotEmpty(devType.getFilters())) {
                List<Integer> filterIds = JSON.parseArray(devType.getFilters(), Integer.class);
                filterIds.forEach(filterInfoId -> {
                    filterInfoList.add(filterInfoService.find4Id(filterInfoId));
                    filterIdList.add(filterInfoId);
                });
            }
            devType.setFilterInfoList(filterInfoList);
            devType.setFilterIdList(filterIdList);

            List<BillSet> billSetList = new ArrayList<>();
            List<Integer> billSetIdList = new ArrayList<>();
            if (StringUtils.isNotEmpty(devType.getBillSets())) {
                List<Integer> filterIds = JSON.parseArray(devType.getBillSets(), Integer.class);
                filterIds.forEach(filterInfoId -> {
                    billSetList.add(billSetService.find4Id(filterInfoId));
                    billSetIdList.add(filterInfoId);
                });
            }
            devType.setBillSetList(billSetList);
            devType.setBillSetIdList(billSetIdList);
        }
        return devType;
    }

    @Override
    public List<DevType> queryList(DevType filter) {
        return mapper.queryList(filter);
    }

    @Override
    public Page<DevType> queryPage(DevType filter, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DevType> page = (Page<DevType>) mapper.queryList(filter);
        page.forEach(devType -> {
            if (StringUtils.isNotEmpty(devType.getFilters())) {
                StringBuffer stringBuffer = new StringBuffer();
                List<Integer> filterIds = JSON.parseArray(devType.getFilters(), Integer.class);
                filterIds.forEach(id -> {
                    stringBuffer.append(filterInfoService.find4Id(id).getName()).append(",");
                });
                devType.setFilterNames(stringBuffer.toString());
            }

            if (StringUtils.isNotEmpty(devType.getBillSets())) {
                StringBuffer stringBuffer = new StringBuffer();
                List<Integer> filterIds = JSON.parseArray(devType.getBillSets(), Integer.class);
                filterIds.forEach(id -> {
                    stringBuffer.append(billSetService.find4Id(id).getName()).append(",");
                });
                devType.setBillSetNames(stringBuffer.toString());
            }
        });
        return page;
    }
}
